package com.laex;

import gov.nist.siplite.stack.Subscription;

import java.util.Vector;

import javax.microedition.rms.RecordEnumeration;
import javax.microedition.rms.RecordStore;
import javax.microedition.rms.RecordStoreException;
import javax.microedition.rms.RecordStoreFullException;
import javax.microedition.rms.RecordStoreNotFoundException;
import javax.microedition.rms.RecordStoreNotOpenException;

import com.laex.model.Expense;

public class RecordUtil {

	private static RecordStore categoryStore = null;
	private static RecordStore expenseStore = null;

	public static final String CATEGORY_STORE = "category";
	public static final String EXPENSE_STORE = "expense";

	public static void insertCategory(String category) {
		try {
			categoryStore = RecordStore.openRecordStore(CATEGORY_STORE, true);
			byte[] catVal = category.getBytes();
			categoryStore.addRecord(catVal, 0, catVal.length);
		} catch (RecordStoreFullException e) {
			e.printStackTrace();
		} catch (RecordStoreNotFoundException e) {
			e.printStackTrace();
		} catch (RecordStoreException e) {
			e.printStackTrace();
		} finally {
			if (categoryStore != null) {
				try {
					categoryStore.closeRecordStore();
				} catch (RecordStoreNotOpenException e) {
					e.printStackTrace();
				} catch (RecordStoreException e) {
					e.printStackTrace();
				}
			}
		}
	}

	public static void insertExpense(Expense expense) {
		try {
			expenseStore = RecordStore.openRecordStore(EXPENSE_STORE, true);
			String concat = expense.getDate().substring(0, 10) + " - " + expense.getAmount() + " - "
					+ expense.getCategory();
			byte[] b = concat.getBytes();
			expenseStore.addRecord(b, 0, b.length);
		} catch (RecordStoreFullException e) {
			e.printStackTrace();
		} catch (RecordStoreNotFoundException e) {
			e.printStackTrace();
		} catch (RecordStoreException e) {
			e.printStackTrace();
		} finally {
			if (expenseStore != null) {
				try {
					expenseStore.closeRecordStore();
				} catch (RecordStoreNotOpenException e) {
					e.printStackTrace();
				} catch (RecordStoreException e) {
					e.printStackTrace();
				}
			}
		}
	}

	public static String[] getExpenseReport() {
		String[] retArr = null;
		try {
			expenseStore = RecordStore.openRecordStore(EXPENSE_STORE, true);
			retArr = new String[expenseStore.getNumRecords()];
			RecordEnumeration re = expenseStore.enumerateRecords(null, null,
					false);
			int i = 0;
			while (re.hasNextElement()) {
				try {
					byte[] b = re.nextRecord();
					String concat = new String(b);
					retArr[i] = concat;
					i++;
				} catch (NullPointerException ne) {
				}
			}
		} catch (RecordStoreFullException e) {
			e.printStackTrace();
		} catch (RecordStoreNotFoundException e) {
			e.printStackTrace();
		} catch (RecordStoreException e) {
			e.printStackTrace();
		} finally {
			if (expenseStore != null) {
				try {
					expenseStore.closeRecordStore();
				} catch (RecordStoreNotOpenException e) {
					e.printStackTrace();
				} catch (RecordStoreException e) {
					e.printStackTrace();
				}
			}
		}
		return retArr;
	}
	
	public static int[] getPieChartData() {
		String[] cats = getCategories();
		String[] sarr = getExpenseReport();
		
		Vector data = new Vector();
		
		double subTotal = 0.0;
		
		for (int i=0; i<cats.length; i++) {
			String cate = cats[i].trim();
			for (int j=0; j<sarr.length; j++) {
				String[] expDtl = split(sarr[j]);
				String expCat = expDtl[2];
				Double expAmt = Double.valueOf(expDtl[1]);
				
				if (cate.equals(expCat)) {
					subTotal += expAmt.doubleValue();
				}
			}
			data.addElement(new Double(subTotal));
			subTotal = 0.0;
		}
		
		int[] datArr = new int[cats.length];
		for (int i=0; i<data.size(); i++) {
			datArr[i] = ((Double)data.elementAt(i)).intValue() / 100;
		}
		
		return datArr;
	}

	public static String[] getCategories() {
		String[] retArr = null;
		try {
			categoryStore = RecordStore.openRecordStore(CATEGORY_STORE, true);
			retArr = new String[categoryStore.getNumRecords()];
			RecordEnumeration re = categoryStore.enumerateRecords(null, null,
					false);
			int i = 0;
			while (re.hasNextElement()) {
				try {
					byte[] data = re.nextRecord();
					retArr[i] = new String(data);
					i++;
				} catch (NullPointerException ne) {

				}
			}
		} catch (RecordStoreNotFoundException e) {
			e.printStackTrace();
		} catch (RecordStoreException e) {
			e.printStackTrace();
		} finally {
			if (categoryStore != null) {
				try {
					categoryStore.closeRecordStore();
				} catch (RecordStoreNotOpenException e) {
					e.printStackTrace();
				} catch (RecordStoreException e) {
					e.printStackTrace();
				}
			}
		}
		return retArr;
	}

	public static void deleteAllExpenses() {
		try {
			RecordStore.deleteRecordStore(EXPENSE_STORE);
		} catch (RecordStoreNotFoundException e) {
			e.printStackTrace();
		} catch (RecordStoreException e) {
			e.printStackTrace();
		}
	}

	public static void deleteAllCategories() {
		try {
			RecordStore.deleteRecordStore(CATEGORY_STORE);
		} catch (RecordStoreNotFoundException e) {
			e.printStackTrace();
		} catch (RecordStoreException e) {
			e.printStackTrace();
		}
	}

	public static String getExpenseTotal() {
		String[] sarr = getExpenseReport();
		double total = 0;
		for (int i = 0; i < sarr.length; i++) {
			String concat = sarr[i];
			String[] cccc = split(concat);
			String amount = cccc[1];
			total += Double.valueOf(amount).doubleValue();
		}
		return "$ " + String.valueOf(total);
	}

	private static String[] split(String original) {
		Vector nodes = new Vector();
		String separator = "-";
		// Parse nodes into vector
		int index = original.indexOf(separator);
		while (index >= 0) {
			nodes.addElement(original.substring(0, index));
			original = original.substring(index + separator.length());
			index = original.indexOf(separator);
		}
		// Get the last node
		nodes.addElement(original);

		// Create splitted string array
		String[] result = new String[nodes.size()];
		if (nodes.size() > 0) {
			for (int loop = 0; loop < nodes.size(); loop++) {
				result[loop] = ((String) nodes.elementAt(loop)).trim();
			}

		}

		return result;
	}

}
